/*
 * Copyright 2013 Sigurd Randoll <srandoll@digiway.de>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.server.infrastructure.persistence;

import de.digiway.rapidbreeze.server.TestUtil;
import de.digiway.rapidbreeze.server.infrastructure.objectstorage.ObjectStorage;
import de.digiway.rapidbreeze.server.model.download.DownloadRepositoryFactory;
import de.digiway.rapidbreeze.server.model.storage.StorageProvider;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;

/**
 *
 * @author Sigurd Randoll <srandoll@digiway.de>
 */
public class StorageProviderRepositoryStorageIT {

    private StorageProviderRepositoryStorage storageProviderRepository;
    private Path storagePath;

    @Before
    public void setup() {
        storagePath = TestUtil.getNonExistingTemporaryPath();
        storageProviderRepository = new StorageProviderRepositoryStorage(new ObjectStorage(storagePath));
        storageProviderRepository.clearAll();
    }

    @After
    public void tearDown() throws IOException {
        TestUtil.deleteDirectoryRecursive(storagePath);
    }

    @Test
    public void testRegisterProvider() {
        assertTrue(storageProviderRepository.getStorageProviders().isEmpty());
        storageProviderRepository.registerProvider("TestProvider", TestProvider.class);

        List<StorageProvider> providers = storageProviderRepository.getStorageProviders();
        assertEquals(1, providers.size());
        assertEquals("TestProvider", providers.get(0).getName());

        try {
            storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
            fail();
        } catch (Exception e) {
        }
    }

    @Test
    public void testUpdateProvider() {
        final String USERNAME = "username";
        final String PASSWORD = "pass";

        StorageProvider provider = new StorageProvider("TestProvider", "class");
        try {
            storageProviderRepository.updateStorageProvider(provider);
            fail();
        } catch (Exception ex) {
        }

        provider = storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        assertNull(provider.getPremiumUsername());
        assertNull(provider.getPremiumPassword());

        provider.setPremiumUsername(USERNAME);
        provider.setPremiumPassword(PASSWORD);
        storageProviderRepository.updateStorageProvider(provider);
        assertEquals(USERNAME, provider.getPremiumUsername());
        assertEquals(PASSWORD, provider.getPremiumPassword());
    }

    @Test
    public void testGetStorageProviders() {
        assertTrue(storageProviderRepository.getStorageProviders().isEmpty());
        StorageProvider storageProvider = storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        assertEquals(1, storageProviderRepository.getStorageProviders().size());
        assertTrue(storageProviderRepository.getStorageProviders().contains(storageProvider));
    }

    @Test
    public void testGetStorageProvider() throws MalformedURLException {
        StorageProvider provider = storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        StorageProvider providerGet = storageProviderRepository.getStorageProvider(new URL("http://" + TestProvider.PROVIDER_HOST));
        assertEquals(provider, providerGet);

        assertNull(storageProviderRepository.getStorageProvider(new URL("http://blabla.test")));
    }

    @Test
    public void testClearAll() {
        assertTrue(storageProviderRepository.getStorageProviders().isEmpty());
        StorageProvider provider = storageProviderRepository.registerProvider("TestProvider", TestProvider.class);
        assertEquals(1, storageProviderRepository.getStorageProviders().size());
        storageProviderRepository.clearAll();
        assertTrue(storageProviderRepository.getStorageProviders().isEmpty());
    }
}
